package com.zqj.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zqj.project.common.annotation.PreventDuplicateSubmit;
import com.zqj.project.common.entity.R;
import com.zqj.project.common.request.PageParams;
import com.zqj.project.converter.ReviewConverter;
import com.zqj.project.dto.ReviewDTO;
import com.zqj.project.service.ReviewService;
import com.zqj.project.vo.ReviewVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * @Description: ReviewController
 * @Author: 22001010704 Zhang Qijian
 * @CreateTime: 2024/12/8 19:38
 * @Version: 1.0
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/review")
@Tag(name = "Review API", description = "评价管理接口，支持增删改查以及分页功能")
@RequiredArgsConstructor
public class ReviewController {

    // 自动生成依赖注入的构造函数
    private final ReviewService reviewService;

    // 分页查询评价列表
    @Operation(summary = "分页查询评价列表", description = "根据分页参数查询评价信息")
    @PostMapping("/page")
    public R<IPage<ReviewVO>> queryReviewPage(@RequestBody @Validated @NotNull @Parameter(description = "分页参数及筛选条件") PageParams<ReviewDTO> pageParams) {
        ReviewDTO reviewDTO = pageParams.getModel();
        if (Objects.isNull(reviewDTO.getUserId()) && Objects.isNull(reviewDTO.getProductId())) {
            return R.fail("用户ID和商品ID不能同时为空");
        }
        return R.success(ReviewConverter.toReviewVoPage(this.reviewService.page(ReviewConverter.toReviewModel(reviewDTO), new Page<>(pageParams.getCurrent(), pageParams.getSize()))));
    }

    // 查询评价详情
    @Operation(summary = "查询评价详情", description = "根据订单ID查询具体评价的详细信息")
    @GetMapping("/info/{orderId}")
    public R<ReviewVO> info(@PathVariable @Parameter(description = "商品id", required = true) Long orderId) {
        return R.success(ReviewConverter.toReviewVo(this.reviewService.info(orderId)));
    }

    // 新增评价
    @Operation(summary = "新增评价", description = "新增一条新的评价记录")
    @PostMapping("/add")
    @PreventDuplicateSubmit
    public R<String> add(@RequestBody @Validated @Parameter(description = "要新增的评价信息") ReviewDTO reviewDto) {
        return Boolean.TRUE.equals(this.reviewService.add(ReviewConverter.toReviewModel(reviewDto))) ? R.success("评价新增成功") : R.fail("评价新增失败");
    }

    // 修改评价
    @Operation(summary = "修改评价", description = "根据评价ID更新评价信息")
    @PutMapping("/edit")
    @PreventDuplicateSubmit
    public R<String> edit(@RequestBody @Validated @Parameter(description = "要修改的评价信息，需包含ID") ReviewDTO reviewDto) {
        if (Objects.isNull(reviewDto.getId())) {
            return R.fail("评价ID不能为空");
        }
        return Boolean.TRUE.equals(this.reviewService.edit(ReviewConverter.toReviewModel(reviewDto))) ? R.success("评价修改成功") : R.fail("评价修改失败");
    }

    // 删除评价
    @Operation(summary = "删除评价", description = "根据评价ID删除评价信息")
    @DeleteMapping("/delete/{id}")
    @PreventDuplicateSubmit
    public R<String> delete(@PathVariable @Parameter(description = "评价ID", required = true) Long id) {
        return Boolean.TRUE.equals(this.reviewService.delete(id)) ? R.success("评价删除成功") : R.fail("评价删除失败");
    }

}